package com.tianbing.adlottery.controller.supplieradmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.entity.Product;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class RegisterProductProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(RegisterProductProfileController.class.getName());


	private SkinUtils skinUtils;

	private UserBusiness userBusiness;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		Product product = new Product();
		fb.setProduct(product);
		return fb;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		ControllerFormBean form = (ControllerFormBean) cmd;

		Product product = form.getProduct();
		product = userBusiness.registerSuppliedProductProfile(auth.getUserSysId(),product);
		ModelAndView mv = new ModelAndView(this.getSuccessView()+"?prdsid="+product.getSysId());
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	public static void setLogger(Logger logger) {
		RegisterProductProfileController.logger = logger;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}


}
